﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PrinteMaster
{
    public partial class 添加打印机 : Form
    {
        public 添加打印机()
        {
            InitializeComponent();
        }

        private void 添加打印机_Load(object sender, EventArgs e)
        {
            uint lastError;
            DataBase.PrinterState state;
            DataSet req;
            DataBase.Error er;
            
            PrinterMasterClient.PrinterAPI.getInstance().enumPrinter(out lastError);
            

            for (int i = 0; i < PrinterMasterClient.PrinterAPI.m_PrinterQueues.Count; i++)
            {
                
                er = DataBase.getInstance().getPrinterState(PrinterMasterClient.PrinterAPI.m_PrinterQueues[i].name, out state, out req);
                if (er == DataBase.Error.printerName)
                {
                    //不存在
                    listBox新加列表.Items.Add(PrinterMasterClient.PrinterAPI.m_PrinterQueues[i].name);
                }
                else
                {
                    //存在
                    listBox已有列表.Items.Add(PrinterMasterClient.PrinterAPI.m_PrinterQueues[i].name);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var index = listBox已有列表.SelectedItems.Count;
            if (index == 0)
            {
                MessageBox.Show("必须在右侧选择一个需要删除的打印机!");
                return;
            }
            DataBase.Error er = DataBase.getInstance().deletePrinter(listBox已有列表.SelectedItem.ToString());
            if (er == DataBase.Error.ok)
            {
                MessageBox.Show("删除打印机成功!");
                listBox已有列表.Items.Remove(listBox已有列表.SelectedItem);
            }
            else
                MessageBox.Show("删除打印机失败，数据库发生错误.");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox新加列表.SelectedItems.Count == 0)
            {
                MessageBox.Show("你必须在左侧列表中选择一个打印机!");
                button添加.Enabled = false;
                return;
            }
            button添加.Enabled = true;
            textBox打印机名字.Text = listBox新加列表.SelectedItem.ToString();
            listBox新加列表.Items.Remove(listBox新加列表.SelectedItem);
        }

        private void textBoxA3_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void textBoxA3_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }

        private void textBoxA4_KeyPress(object sender, KeyPressEventArgs e)
        {
            Function.inputInt(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataBase.PrinterState p = new DataBase.PrinterState();
            p.printerName = textBox打印机名字.Text;
            p.rgbA3Money = Convert.ToDouble(textBoxA4.Text) / 100;
            p.rgbMoney = Convert.ToDouble(textBoxA3.Text) / 100;
            p.blackA3Money = p.rgbA3Money;
            p.blackMoney = p.rgbMoney;
            if (comboBox颜色.Text == "彩色")
                p.color = DataBase.PrinterState.color颜色.c彩色;
            else
                p.color = DataBase.PrinterState.color颜色.b黑白;
            DataBase.Error er = DataBase.getInstance().addPrinter(p);
            if (textBox打印机名字.Text == "")
            {
                MessageBox.Show("必须在左侧选择一个需要添加的打印机!");
            }
            if (er == DataBase.Error.ok)
            {
                MessageBox.Show("添加成功!");
                this.Close();
            }
            else
                MessageBox.Show("添加失败=>" + er);
        }

        private void button提到修改_Click(object sender, EventArgs e)
        {
            var index = listBox已有列表.SelectedItems.Count;
            if (index == 0)
            {
                MessageBox.Show("必须在右侧选择一个需要修改的打印机!");
                button修改.Enabled = false;
                return;
            }
            textBox打印机名字.Text = listBox已有列表.SelectedItem.ToString();
            var A3 = 0.0; var A4 = 0.0;
            DataBase.getInstance().getMoneyForPage(textBox打印机名字.Text, out A4, out A3);
            textBoxA3.Text = (A3*100.0).ToString("0.00");
            textBoxA4.Text = (A4*100.0).ToString("0.00");
            button修改.Enabled = true;

        }

        private void button修改_Click(object sender, EventArgs e)
        {
            if (textBox打印机名字.Text == "")
            {
                MessageBox.Show("必须在右侧选择一个需要修改的打印机!");
            }
            DataBase.PrinterState p = new DataBase.PrinterState();
            p.printerName = textBox打印机名字.Text;
            p.rgbA3Money = Convert.ToDouble(textBoxA3.Text) / 100;
            p.rgbMoney = Convert.ToDouble(textBoxA4.Text) / 100;
            p.blackA3Money = p.rgbA3Money;
            p.blackMoney = p.rgbMoney;
            if (comboBox颜色.Text == "彩色")
                p.color = DataBase.PrinterState.color颜色.c彩色;
            else
                p.color = DataBase.PrinterState.color颜色.b黑白;
            DataBase.Error er = DataBase.getInstance().updatePrinter(p);
            if (er == DataBase.Error.ok)
            {
                MessageBox.Show("修改成功!");
                this.Close();
            }
            else
                MessageBox.Show("修改失败=>" + er);
        }
    }
}
